﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ASPNETPerformance._06_Cache;
using ASPNETPerformance.App_BusinessLogic.Cache;
using ASPNETPerformance.App_BusinessLogic.StaticPage;
using HttpModuleFactory = System.Func<System.Web.HttpApplication, System.Web.IHttpModule>;

namespace ASPNETPerformance {
    public class Global : System.Web.HttpApplication {
        public static DataCacheManager DataCache = new DataCacheManager();
        public static RequestCacheManager RequestCache = new RequestCacheManager();
        public static FileCacheManager FileCache = new FileCacheManager();

        void Application_Start( object sender, EventArgs e ) {
            // 在应用程序启动时运行的代码

        }

        void Application_BeginRequest( object sender, EventArgs e ) {

            Global.RequestCache.Add( "RequestCache",
                StaticPageManager.GetGenerationProductsData().Find( f => f.ItemCode == "07-015-000AC" ) );

        }

        void Application_End( object sender, EventArgs e ) {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error( object sender, EventArgs e ) {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start( object sender, EventArgs e ) {
            // 在新会话启动时运行的代码

        }

        void Session_End( object sender, EventArgs e ) {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer，则不会引发该事件。

        }


        #region Dynamic HttpApplication

        private static readonly List<HttpModuleFactory> _moduleFactories = new List<HttpModuleFactory>();
        private static bool _started;

        private List<IHttpModule> _dynamicModules;

        public override void Dispose() {
            if ( _dynamicModules != null ) {
                foreach ( IHttpModule module in _dynamicModules ) {
                    module.Dispose();
                }
                _dynamicModules = null;
            }

            base.Dispose();
        }

        public override void Init() {
            _started = true;

            base.Init();

            if ( _moduleFactories.Count != 0 ) {
                List<IHttpModule> dynamicModules = new List<IHttpModule>();
                foreach ( HttpModuleFactory moduleFactory in _moduleFactories ) {
                    IHttpModule module = moduleFactory( this );
                    if ( module != null ) {
                        module.Init( this );
                        dynamicModules.Add( module );
                    }
                }

                if ( dynamicModules.Count != 0 ) {
                    _dynamicModules = dynamicModules;
                }
            }
        }

        public static void RegisterModule( HttpModuleFactory moduleFactory ) {
            if ( moduleFactory == null ) {
                throw new ArgumentNullException( "moduleFactory" );
            }
            if ( _started ) {
                throw new InvalidOperationException( "Any modules must be registered before the application starts." );
            }

            _moduleFactories.Add( moduleFactory );
        }
        #endregion
    }
}
